package com.cybertron.ironhide.search.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cybertron.ironhide.search.common.base.Pager;
import com.cybertron.ironhide.search.domain.Comment;
import com.cybertron.ironhide.search.domain.Member;
import com.cybertron.ironhide.search.persistence.CommentMapper;
import com.cybertron.ironhide.search.persistence.MemberMapper;
import com.cybertron.ironhide.search.service.CommentService;
import com.cybertron.ironhide.search.util.CommonUtil;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired(required = false)
	private CommentMapper commentMapper;

	@Autowired(required = false)
	private MemberMapper memberMapper;

	@Override
	public List<Comment> selectByArticleId(String id, Pager pager) {
		return commentMapper.selectByArticleId(id, pager);
	}

	@Override
	public void insertSelective(Comment record) {
		record.setId(CommonUtil.getUUID());
		record.setCreateDate(new Date());
		record.setModifyDate(new Date());
		Member member = record.getMember();
		if (member == null) {
			return;
		}
		Member persistMember = memberMapper.selectByEmail(member.getEmail());
		if (persistMember != null) {
			persistMember.setName(member.getName());
			persistMember.setModifyDate(new Date());
			record.setMemberId(persistMember.getId());
			memberMapper.updateByPrimaryKeySelective(persistMember);
		} else {
			persistMember = new Member();
			persistMember.setId(CommonUtil.getUUID());
			persistMember.setName(member.getName());
			persistMember.setModifyDate(new Date());
			persistMember.setCreateDate(new Date());
			persistMember.setEmail(member.getEmail());
			record.setMemberId(persistMember.getId());
			memberMapper.insertSelective(persistMember);
		}
		commentMapper.insertSelective(record);
	}
}
